package com.in.mobilerechargedth;

import java.util.List;


import com.in.mobilerechargedth.adapter.FifthScreenListViewAdapter;
import com.in.mobilerechargedth.api.CircleApi;
import com.in.mobilerechargedth.constant.ConstantVariables;
import com.in.mobilerechargedth.model.CircleModel;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;

import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class FifthScreenActivity extends BaseActivity {

    public static List<CircleModel> listCircle;
    ListView lvCircle;
    TextView tvMoreDetail;
    Button backbtn;
    FifthScreenListViewAdapter adapter;
    int operatorId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fifth_screen);

        String strOperatorId = getIntent().getStringExtra(ConstantVariables.OPERATOR_ID);
        operatorId = Integer.parseInt(strOperatorId);
        lvCircle = (ListView) findViewById(R.id.lvCircle);
        tvMoreDetail =(TextView) findViewById(R.id.tvMoreDetail);
        tvMoreDetail.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent i = new Intent(FifthScreenActivity.this,
                        FifthMoreDetailScreenActivity.class);
                i.putExtra(ConstantVariables.OPERATOR_ID, operatorId + "");
                startActivity(i);
            }
        });
        backbtn = (Button) findViewById(R.id.backbtn);
        backbtn.setVisibility(View.VISIBLE);
        backbtn.setOnClickListener(this);

        new ChannelDetailTask().execute(new String [] {});
        setupAdmob();
        share();
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
    }
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        super.onClick(v);
        switch (v.getId()) {
        case R.id.backbtn:
            this.onBackPressed();
            break;
        }
    }


    /**
     * Async Task to proceed Operator.
     */
    private class ChannelDetailTask extends AsyncTask<String, Void, List<CircleModel>> {

        private ProgressDialog mProgress;


        @Override
        protected void onPreExecute() {
            mProgress = new ProgressDialog(FifthScreenActivity.this);
            mProgress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            mProgress.setMessage("Loading...");
            mProgress.setCanceledOnTouchOutside(false);
            // Check press back key when showing dialog.
            mProgress.setOnKeyListener(new DialogInterface.OnKeyListener() {

                @Override
                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                    // TODO Auto-generated method stub
                    if (keyCode == KeyEvent.KEYCODE_BACK) {
                        return true;
                    }
                    return false;
                }
            });
            mProgress.show();

            super.onPreExecute();
        }

        @Override
        protected List<CircleModel> doInBackground(String... params) {
            CircleApi api = new CircleApi();
            List<CircleModel> data = null;
            try {
                data = api.getListCircle(operatorId);
            } catch (Exception e) {
                Log.e("Exception", e.getMessage());
            }

            return data;
        }

        @Override
        protected void onPostExecute(List<CircleModel> result) {
            super.onPostExecute(result);

            if (result != null) {
                listCircle = result;
                adapter = new FifthScreenListViewAdapter(FifthScreenActivity.this, result);
                lvCircle.setAdapter(adapter);

                lvCircle.setOnItemClickListener(new OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> arg0, View arg1,
                            int position, long id) {
                        Intent i = new Intent(FifthScreenActivity.this,
                                FifthCircleDetailScreenActivity.class);
                        i.putExtra(ConstantVariables.OPERATOR_ID, operatorId + "");
                        i.putExtra(ConstantVariables.CIRCLE_ID, adapter.getItemId(position) + "");
                        startActivity(i);

                    }
                });
            }
            if (mProgress != null) {
                mProgress.dismiss();
                mProgress = null;
            }

        }

    }
}
